package be.vdab.servlets;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.DAO.PizzaDAO;
import be.vdab.entities.Pizza;
import be.vdab.util.RequestStatistiek;

@WebServlet("/pizzas.htm")
public class PizzaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/pizzas.jsp";
	private final RequestStatistiek requestStatistiek = new RequestStatistiek();
	private final PizzaDAO pizzaDAO = new PizzaDAO();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		requestStatistiek.voegRequestToe(request);
		request.setAttribute("sterren", 5);
	Iterable<Pizza> pizzas = pizzaDAO.findAll();
	String pizzaFotosPad = this.getServletContext().getRealPath("/pizzafotos");
	Set<Long> pizzaNummersMetFoto = new HashSet<>();
	for (Pizza pizza : pizzas) {
		File file = new File(pizzaFotosPad + "/" + pizza.getNummer() + ".jpg");
		if (file.exists()) { //is er een foto voor deze pizza?
			pizzaNummersMetFoto.add(pizza.getNummer());
		}
	}
	request.setAttribute("pizzas", pizzas);
	request.setAttribute("pizzaNummersMetFoto", pizzaNummersMetFoto);
	RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
	dispatcher.forward(request, response);
	}

}
